#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int MyCircularqueueDataType;


typedef struct {
    MyCircularqueueDataType* a;
    int Front;
    int Rear;
    int capacity;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* newMCQ = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if (newMCQ == NULL)
    {
        perror("malloc");
        return NULL;
    }
    newMCQ->capacity = k + 1;
    MyCircularqueueDataType* newque = (MyCircularqueueDataType*)malloc(sizeof(MyCircularqueueDataType) * newMCQ->capacity);
    if (newque == NULL)
    {
        perror("malloc");
        return NULL;
    }
    newMCQ->a = newque;
    newMCQ->Front = 0;
    newMCQ->Rear = 0;
    return newMCQ;
}
bool myCircularQueueIsFull(MyCircularQueue* obj);
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
        return false;
    obj->a[obj->Rear] = value;
    obj->Rear++;
    if (obj->Rear == obj->capacity)
    {
        obj->Rear = 0;
    }
    return true;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (!myCircularQueueIsEmpty(obj))
    {
        obj->Front++;
        if (obj->Front == obj->capacity)
        {
            obj->Front = 0;
        }

        return true;
    }
    return false;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (!myCircularQueueIsEmpty(obj))
    {
        return obj->a[obj->Front];
    }
    else
    {
        return -1;
    }
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (!myCircularQueueIsEmpty(obj))
    {
        if (obj->Rear - 1 == -1)
        {
            return obj->a[obj->capacity - 1];
        }
        else
        {
            return obj->a[obj->Rear - 1];
        }
    }
    return -1;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->Front == obj->Rear;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return ((obj->Rear - obj->Front) + obj->capacity) % obj->capacity == obj->capacity - 1;
}

void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->a);
    obj->Front = 0;
    obj->Rear = 0;
    obj->capacity = 0;
    free(obj);

}